package com.mycompany.rest.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.mycompany.rest.domain.Artista;
import com.mycompany.rest.domain.Person;

@Service("artistaService")
public class ArtistaService {

protected static Logger logger = Logger.getLogger("service");
	
	// In-memory list
	private List<Artista> artistas = new ArrayList<Artista>();
	
	public ArtistaService() {
		logger.debug("Init database");
		
		// Create in-memory list
		Artista a1= new Artista();
		a1.setNumero((long)500);
		a1.setNombre("melcochita");
		
		Artista a2= new Artista();
		a2.setNumero((long)400);
		a2.setNombre("Josie");
		
		artistas.add(a1);
		artistas.add(a2);
	}
	
	/**
	 * Retrieves all persons
	 */
	public List<Artista> getAll() {
		logger.debug("Retrieving all persons");
		
		return artistas;
	}
	
	/**
	 * Retrieves a single person
	 */
	public Artista get( Long id ) {
		logger.debug("Retrieving person with id: " + id);
		
		for (Artista artista:artistas) {
			if (artista.getNumero().longValue() == id.longValue()) {
				logger.debug("Found record");
				return artista;
			}
		}
		
		logger.debug("No records found");
		return null;
	}
	
	/**
	 * Adds a new person
	 */
	public Artista add(Artista artista) {
		logger.debug("Adding new person");
		
		try {
			// Find suitable id
			Long newId = 0L;
			Boolean hasFoundSuitableId = false;
			while(hasFoundSuitableId == false) {
				for (int i=0; i <artistas.size(); i++) {
					if (artista.getNumero().longValue() == newId.longValue()) {
						newId++;
						break;
					}
					
					// Exit while loop
					if(i==artistas.size()-1) {
						logger.debug("Assigning id: " + newId);
						hasFoundSuitableId = true;
					}
				}
			}
			
			// Assign new id
			//artista.setNumero(newId);			
			// Add to list
			artistas.add(artista);
			
			logger.debug("Added new person");
			return artista;
		} catch (Exception e) {
			logger.error(e);
			return null;
		}
	}
	
	/**
	 * Edits an existing person
	 */
	public Boolean edit(Artista artista) {
		logger.debug("Editing person with id: " + artista.getNumero());
		
		try {
			for (Artista p:artistas) {
				if (p.getNumero().longValue() == artista.getNumero().longValue()) {
					logger.debug("Found record");
					artistas.remove(p);
					artistas.add(artista);
					return true;
				}
			}
			
			logger.debug("No records found");
			return false;
			
		} catch (Exception e) {
			logger.error(e);
			return false;
		}
	}
	
	/**
	 * Deletes an existing person
	 */
	public Boolean delete(Long numero) {
		logger.debug("Deleting person with id: " + numero);
		
		try {
			for (Artista artista:artistas) {
				if (artista.getNumero().longValue() == numero.longValue()) {
					logger.debug("Found record");
					artistas.remove(artista);
					return true;
				}
			}
			
			logger.debug("No records found");
			return false;
			
		} catch (Exception e) {
			logger.error(e);
			return false;
		}
	}
}
